home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevwprn.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  19.3 KB  |  689 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevwprn.c,v 1.2 2000/09/19 19:00:23 lpd Exp $ */
  20. /*
  21.  * Microsoft Windows 3.n printer driver for Ghostscript.
  22.  *
  23.  * Original version by Russell Lang and
  24.  * L. Peter Deutsch, Aladdin Enterprises.
  25.  */
  26. #include "gdevmswn.h"
  27. #include "gp.h"
  28. #include "commdlg.h"
  29.  
  30. /*
  31.  ****** NOTE: this module and gdevwddb should be refactored.
  32.  * The drawing routines are almost identical.
  33.  * The differences are that the mswinprn doesn't use an extra
  34.  * palette (gdevwddb.c could probably be made to work with 
  35.  * one palette also), mswinprn doesn't call win_update() because
  36.  * hwndimg doesn't exist, and the HDC is hdcmf not hdcbit.
  37.  ******/
  38.  
  39. /* Make sure we cast to the correct structure type. */
  40. typedef struct gx_device_win_prn_s gx_device_win_prn;
  41.  
  42. #undef wdev
  43. #define wdev ((gx_device_win_prn *)dev)
  44.  
  45. /* Forward references */
  46. private void near win_prn_addtool(P2(gx_device_win_prn *, int));
  47. private void near win_prn_maketools(P2(gx_device_win_prn *, HDC));
  48. private void near win_prn_destroytools(P1(gx_device_win_prn *));
  49.  
  50. /* Device procedures */
  51.  
  52. /* See gxdevice.h for the definitions of the procedures. */
  53. private dev_proc_open_device(win_prn_open);
  54. private dev_proc_close_device(win_prn_close);
  55. private dev_proc_sync_output(win_prn_sync_output);
  56. private dev_proc_output_page(win_prn_output_page);
  57. private dev_proc_map_rgb_color(win_prn_map_rgb_color);
  58. private dev_proc_fill_rectangle(win_prn_fill_rectangle);
  59. private dev_proc_tile_rectangle(win_prn_tile_rectangle);
  60. private dev_proc_copy_mono(win_prn_copy_mono);
  61. private dev_proc_copy_color(win_prn_copy_color);
  62. private dev_proc_draw_line(win_prn_draw_line);
  63.  
  64. /* The device descriptor */
  65. struct gx_device_win_prn_s {
  66.     gx_device_common;
  67.     gx_device_win_common;
  68.  
  69.     /* Handles */
  70.  
  71.     HPEN hpen, *hpens;
  72.     uint hpensize;
  73.     HBRUSH hbrush, *hbrushs;
  74.     uint hbrushsize;
  75. #define select_brush(color)\
  76.   if (wdev->hbrush != wdev->hbrushs[color])\
  77.    {    wdev->hbrush = wdev->hbrushs[color];\
  78.     SelectObject(wdev->hdcmf,wdev->hbrush);\
  79.    }
  80.     /* A staging bitmap for copy_mono. */
  81.     /* We want one big enough to handle the standard 16x16 halftone; */
  82.     /* this is also big enough for ordinary-size characters. */
  83.  
  84. #define bmWidthBytes 4        /* must be even */
  85. #define bmWidthBits (bmWidthBytes * 8)
  86. #define bmHeight 32
  87.     HBITMAP FAR hbmmono;
  88.     HDC FAR hdcmono;
  89.     gx_bitmap_id bm_id;
  90.  
  91.     HDC hdcprn;
  92.     HDC hdcmf;
  93.     char mfname[gp_file_name_sizeof];
  94.     DLGPROC lpfnAbortProc;
  95. };
  96. private const gx_device_procs win_prn_procs =
  97. {
  98.     win_prn_open,
  99.     NULL,            /* get_initial_matrix */
  100.     win_prn_sync_output,
  101.     win_prn_output_page,
  102.     win_prn_close,
  103.     win_prn_map_rgb_color,
  104.     win_map_color_rgb,
  105.     win_prn_fill_rectangle,
  106.     win_prn_tile_rectangle,
  107.     win_prn_copy_mono,
  108.     win_prn_copy_color,
  109.     win_prn_draw_line,
  110.     NULL,            /* get_bits */
  111.     NULL,            /* get_params */
  112.     NULL,            /* put_params */
  113.     NULL,            /* map_cmyk_color */
  114.     win_get_xfont_procs
  115. };
  116. gx_device_win_prn far_data gs_mswinprn_device =
  117. {
  118.     std_device_std_body(gx_device_win_prn, &win_prn_procs, "mswinprn",
  119.             INITIAL_WIDTH, INITIAL_HEIGHT,    /* win_open() fills these in later */
  120.             INITIAL_RESOLUTION, INITIAL_RESOLUTION    /* win_open() fills these in later */
  121.     ),
  122.     {0},            /* std_procs */
  123.     0,                /* BitsPerPixel */
  124.     2,                /* nColors */
  125. };
  126.  
  127. /* Open the win_prn driver */
  128. private int
  129. win_prn_open(gx_device * dev)
  130. {
  131.     int depth;
  132.     PRINTDLG pd;
  133.     FILE *f;
  134.     POINT offset;
  135.     POINT size;
  136.     float m[4];
  137.  
  138.     memset(&pd, 0, sizeof(PRINTDLG));
  139.     pd.lStructSize = sizeof(PRINTDLG);
  140.     pd.hwndOwner = hwndtext;
  141.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  142.     if (!PrintDlg(&pd)) {
  143.     /* device not opened - exit ghostscript */
  144.     return gs_error_limitcheck;
  145.     }
  146.     GlobalFree(pd.hDevMode);
  147.     GlobalFree(pd.hDevNames);
  148.     pd.hDevMode = pd.hDevNames = NULL;
  149.     wdev->hdcprn = pd.hDC;
  150.     if (!(GetDeviceCaps(wdev->hdcprn, RASTERCAPS) != RC_BITBLT)) {
  151.     DeleteDC(wdev->hdcprn);
  152.     return gs_error_limitcheck;
  153.     }
  154. #ifdef __WIN32__
  155.     wdev->lpfnAbortProc = (DLGPROC) AbortProc;
  156. #else
  157. #ifdef __DLL__
  158.     wdev->lpfnAbortProc = (DLGPROC) GetProcAddress(phInstance, "AbortProc");
  159. #else
  160.     wdev->lpfnAbortProc = (DLGPROC) MakeProcInstance((FARPROC) AbortProc, phInstance);
  161. #endif
  162. #endif
  163.     Escape(wdev->hdcprn, SETABORTPROC, 0, (LPSTR) wdev->lpfnAbortProc, NULL);
  164.     if (Escape(wdev->hdcprn, STARTDOC, strlen(szAppName), szAppName, NULL) <= 0) {
  165. #if !defined(__WIN32__) && !defined(__DLL__)
  166.     FreeProcInstance((FARPROC) wdev->lpfnAbortProc);
  167. #endif
  168.     DeleteDC(wdev->hdcprn);
  169.     return gs_error_limitcheck;
  170.     }
  171.     f = gp_open_scratch_file(gp_scratch_file_name_prefix,
  172.                  wdev->mfname, "wb");
  173.     if (f == (FILE *) NULL) {
  174.     Escape(wdev->hdcprn, ENDDOC, 0, NULL, NULL);
  175. #if !defined(__WIN32__) && !defined(__DLL__)
  176.     FreeProcInstance((FARPROC) wdev->lpfnAbortProc);
  177. #endif
  178.     DeleteDC(wdev->hdcprn);
  179.     return gs_error_limitcheck;
  180.     }
  181.     unlink(wdev->mfname);
  182.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  183.  
  184.     dev->x_pixels_per_inch = (float)GetDeviceCaps(wdev->hdcprn, LOGPIXELSX);
  185.     dev->y_pixels_per_inch = (float)GetDeviceCaps(wdev->hdcprn, LOGPIXELSY);
  186.     Escape(wdev->hdcprn, GETPHYSPAGESIZE, 0, NULL, (LPPOINT) & size);
  187.     dev->width = size.x;
  188.     dev->height = size.y;
  189.     Escape(wdev->hdcprn, GETPRINTINGOFFSET, 0, NULL, (LPPOINT) & offset);
  190.     m[0] /*left */  = offset.x / dev->x_pixels_per_inch;
  191.     m[3] /*top */  = offset.y / dev->y_pixels_per_inch;
  192.     m[2] /*right */  =
  193.     (size.x - offset.x - GetDeviceCaps(wdev->hdcprn, HORZRES))
  194.     / dev->x_pixels_per_inch;
  195.     m[1] /*bottom */  =
  196.     (size.y - offset.y - GetDeviceCaps(wdev->hdcprn, VERTRES))
  197.     / dev->y_pixels_per_inch
  198.     + 0.15;            /* hack to add a bit more margin for deskjet printer */
  199.     gx_device_set_margins(dev, m, true);
  200.  
  201.     /* Set parameters that were unknown before opening device */
  202.     /* Find out if the device supports color */
  203.     /* We recognize 2, 16 or 256 color devices */
  204.     depth = GetDeviceCaps(wdev->hdcprn, PLANES) * GetDeviceCaps(wdev->hdcprn, BITSPIXEL);
  205.     if (depth >= 8) {        /* use 64 static colors and 166 dynamic colors from 8 planes */
  206.     static const gx_device_color_info win_256color = dci_color(8, 31, 4);
  207.  
  208.     dev->color_info = win_256color;
  209.     wdev->nColors = 64;
  210.     } else if (depth >= 4) {
  211.     static const gx_device_color_info win_16ega_color = dci_color(4, 2, 3);
  212.  
  213.     dev->color_info = win_16ega_color;
  214.     wdev->nColors = 16;
  215.     } else {            /* default is black_and_white */
  216.     wdev->nColors = 2;
  217.     }
  218.  
  219.     /* create palette for display */
  220.     if ((wdev->limgpalette = win_makepalette((gx_device_win *) dev))
  221.     == (LPLOGPALETTE) NULL) {
  222.     HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  223.  
  224.     DeleteMetaFile(hmf);
  225.     unlink(wdev->mfname);
  226.     Escape(wdev->hdcprn, ENDDOC, 0, NULL, NULL);
  227. #if !defined(__WIN32__) && !defined(__DLL__)
  228.     FreeProcInstance((FARPROC) wdev->lpfnAbortProc);
  229. #endif
  230.     DeleteDC(wdev->hdcprn);
  231.     return win_nomemory();
  232.     }
  233.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  234.  
  235.     /* Create the bitmap and DC for copy_mono. */
  236.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  237.     wdev->hdcmono = CreateCompatibleDC(wdev->hdcprn);
  238.     if (wdev->hbmmono == NULL || wdev->hdcmono == NULL) {
  239.     HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  240.  
  241.     DeleteMetaFile(hmf);
  242.     unlink(wdev->mfname);
  243.     Escape(wdev->hdcprn, ENDDOC, 0, NULL, NULL);
  244. #if !defined(__WIN32__) && !defined(__DLL__)
  245.     FreeProcInstance((FARPROC) wdev->lpfnAbortProc);
  246. #endif
  247.     DeleteDC(wdev->hdcprn);
  248.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) +
  249.         (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  250.         "win_prn_open");
  251.     return win_nomemory();
  252.     }
  253.     SetMapMode(wdev->hdcmono, GetMapMode(wdev->hdcprn));
  254.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  255.     (void)SelectPalette(wdev->hdcmf, wdev->himgpalette, FALSE);
  256.     RealizePalette(wdev->hdcmf);
  257.     win_prn_maketools(wdev, wdev->hdcmf);
  258.     wdev->bm_id = gx_no_bitmap_id;
  259.  
  260.     return 0;
  261. }
  262.  
  263.  
  264. /* Close the win_prn driver */
  265. private int
  266. win_prn_close(gx_device * dev)
  267. {
  268.     HMETAFILE hmf;
  269.  
  270.     /* Free resources */
  271.     Escape(wdev->hdcprn, ENDDOC, 0, NULL, NULL);
  272. #if !defined(__WIN32__) && !defined(__DLL__)
  273.     FreeProcInstance((FARPROC) wdev->lpfnAbortProc);
  274. #endif
  275.     DeleteDC(wdev->hdcprn);
  276.     hmf = CloseMetaFile(wdev->hdcmf);
  277.     DeleteMetaFile(hmf);
  278.     unlink(wdev->mfname);
  279.  
  280.     win_prn_destroytools(wdev);
  281.     DeleteDC(wdev->hdcmono);
  282.     DeleteObject(wdev->hbmmono);
  283.     DeleteObject(wdev->himgpalette);
  284.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) +
  285.         (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  286.         "win_prn_close");
  287.     return (0);
  288. }
  289.  
  290. /* Do nothing */
  291. int
  292. win_prn_sync_output(gx_device * dev)
  293. {
  294.     return 0;
  295. }
  296.  
  297. /* Write page to printer */
  298. int
  299. win_prn_output_page(gx_device * dev, int num_copies, int flush)
  300. {
  301.     RECT rect;
  302.     HMETAFILE hmf;
  303.  
  304.     hmf = CloseMetaFile(wdev->hdcmf);
  305.  
  306.     Escape(wdev->hdcprn, NEXTBAND, 0, NULL, (LPRECT) & rect);
  307.     while (!IsRectEmpty(&rect)) {
  308.     PlayMetaFile(wdev->hdcprn, hmf);
  309.     if (Escape(wdev->hdcprn, NEXTBAND, 0, NULL, (LPRECT) & rect) <= 0)
  310.         break;
  311.     }
  312.     DeleteMetaFile(hmf);
  313.     unlink(wdev->mfname);
  314.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  315.     (void)SelectPalette(wdev->hdcmf, wdev->himgpalette, FALSE);
  316.     RealizePalette(wdev->hdcmf);
  317.     SelectObject(wdev->hdcmf, wdev->hpen);
  318.     SelectObject(wdev->hdcmf, wdev->hbrush);
  319.  
  320.     return gx_finish_output_page(dev, num_copies, flush);
  321. }
  322.  
  323.  
  324. /* Map a r-g-b color to the colors available under Windows */
  325. private gx_color_index
  326. win_prn_map_rgb_color(gx_device * dev, gx_color_value r, gx_color_value g,
  327.               gx_color_value b)
  328. {
  329.     int i = wdev->nColors;
  330.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  331.  
  332.     if (color != i)
  333.     return color;
  334.     (void)SelectPalette(wdev->hdcmf, wdev->himgpalette, FALSE);
  335.     RealizePalette(wdev->hdcmf);
  336.     win_prn_addtool(wdev, i);
  337.  
  338.     return color;
  339. }
  340.  
  341.  
  342. /* Macro for filling a rectangle with a color. */
  343. /* Note that it starts with a declaration. */
  344. #define fill_rect(x, y, w, h, color)\
  345. RECT rect;\
  346. rect.left = x, rect.top = y;\
  347. rect.right = x + w, rect.bottom = y + h;\
  348. FillRect(wdev->hdcmf, &rect, wdev->hbrushs[(int)color])
  349.  
  350.  
  351. /* Fill a rectangle. */
  352. private int
  353. win_prn_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  354.                gx_color_index color)
  355. {
  356.     fit_fill(dev, x, y, w, h);
  357.     /* Use PatBlt for filling.  Special-case black. */
  358.     if (color == 0)
  359.     PatBlt(wdev->hdcmf, x, y, w, h, rop_write_0s);
  360.     else {
  361.     select_brush((int)color);
  362.     PatBlt(wdev->hdcmf, x, y, w, h, rop_write_pattern);
  363.     }
  364.  
  365.     return 0;
  366. }
  367.  
  368. /* Tile a rectangle.  If neither color is transparent, */
  369. /* pre-clear the rectangle to color0 and just tile with color1. */
  370. /* This is faster because of how win_copy_mono is implemented. */
  371. /* Note that this also does the right thing for colored tiles. */
  372. private int
  373. win_prn_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
  374.       int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  375.                int px, int py)
  376. {
  377.     fit_fill(dev, x, y, w, h);
  378.     if (czero != gx_no_color_index && cone != gx_no_color_index) {
  379.     fill_rect(x, y, w, h, czero);
  380.     czero = gx_no_color_index;
  381.     }
  382.     if (tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  383.     (px | py) == 0 && cone != gx_no_color_index
  384.     ) {            /* We can do this much more efficiently */
  385.     /* by using the internal algorithms of copy_mono */
  386.     /* and gx_default_tile_rectangle. */
  387.     int width = tile->size.x;
  388.     int height = tile->size.y;
  389.     int rwidth = tile->rep_width;
  390.     int irx = ((rwidth & (rwidth - 1)) == 0 ?    /* power of 2 */
  391.            x & (rwidth - 1) :
  392.            x % rwidth);
  393.     int ry = y % tile->rep_height;
  394.     int icw = width - irx;
  395.     int ch = height - ry;
  396.     int ex = x + w, ey = y + h;
  397.     int fex = ex - width, fey = ey - height;
  398.     int cx, cy;
  399.  
  400.     select_brush((int)cone);
  401.  
  402.     if (tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id) {
  403.         wdev->bm_id = tile->id;
  404.         SetBitmapBits(wdev->hbmmono,
  405.               (DWORD) (bmWidthBytes * tile->size.y),
  406.               (BYTE *) tile->data);
  407.     }
  408. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  409.   BitBlt(wdev->hdcmf, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  410.  
  411.     if (ch > h)
  412.         ch = h;
  413.     for (cy = y;;) {
  414.         if (w <= icw)
  415.         copy_tile(irx, ry, x, cy, w, ch);
  416.         else {
  417.         copy_tile(irx, ry, x, cy, icw, ch);
  418.         cx = x + icw;
  419.         while (cx <= fex) {
  420.             copy_tile(0, ry, cx, cy, width, ch);
  421.             cx += width;
  422.         }
  423.         if (cx < ex) {
  424.             copy_tile(0, ry, cx, cy, ex - cx, ch);
  425.         }
  426.         }
  427.         if ((cy += ch) >= ey)
  428.         break;
  429.         ch = (cy > fey ? ey - cy : height);
  430.         ry = 0;
  431.     }
  432.  
  433.     return 0;
  434.     }
  435.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  436. }
  437.  
  438.  
  439. /* Draw a line */
  440. private int
  441. win_prn_draw_line(gx_device * dev, int x0, int y0, int x1, int y1,
  442.           gx_color_index color)
  443. {
  444.     if (wdev->hpen != wdev->hpens[(int)color]) {
  445.     wdev->hpen = wdev->hpens[(int)color];
  446.     SelectObject(wdev->hdcmf, wdev->hpen);
  447.     }
  448. #ifdef __WIN32__
  449.     MoveToEx(wdev->hdcmf, x0, y0, NULL);
  450. #else
  451.     MoveTo(wdev->hdcmf, x0, y0);
  452. #endif
  453.     LineTo(wdev->hdcmf, x1, y1);
  454.     return 0;
  455. }
  456.  
  457. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  458. /* Color = gx_no_color_index means transparent (no effect on the image). */
  459. private int
  460. win_prn_copy_mono(gx_device * dev,
  461.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  462.           int x, int y, int w, int h,
  463.           gx_color_index zero, gx_color_index one)
  464. {
  465.     int endx;
  466.     const byte *ptr_line;
  467.     int width_bytes, height;
  468.     DWORD rop = rop_write_at_1s;
  469.     int color;
  470.     BYTE aBit[bmWidthBytes * bmHeight];
  471.     BYTE *aptr = aBit;
  472.  
  473.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  474.  
  475.     if (sourcex & ~7) {
  476.     base += sourcex >> 3;
  477.     sourcex &= 7;
  478.     }
  479.     /* Break up large transfers into smaller ones. */
  480.     while ((endx = sourcex + w) > bmWidthBits) {
  481.     int lastx = (endx - 1) & -bmWidthBits;
  482.     int subw = endx - lastx;
  483.     int code = win_prn_copy_mono(dev, base, lastx,
  484.                      raster, gx_no_bitmap_id,
  485.                      x + lastx - sourcex, y,
  486.                      subw, h, zero, one);
  487.  
  488.     if (code < 0)
  489.         return code;
  490.     w -= subw;
  491.     }
  492.     while (h > bmHeight) {
  493.     int code;
  494.  
  495.     h -= bmHeight;
  496.     code = win_prn_copy_mono(dev, base + h * raster, sourcex,
  497.                  raster, gx_no_bitmap_id,
  498.                  x, y + h, w, bmHeight, zero, one);
  499.     if (code < 0)
  500.         return code;
  501.     }
  502.  
  503.     width_bytes = (sourcex + w + 7) >> 3;
  504.     ptr_line = base;
  505.  
  506.     if (zero == gx_no_color_index) {
  507.     if (one == gx_no_color_index)
  508.         return 0;
  509.     color = (int)one;
  510.     if (color == 0)
  511.         rop = rop_write_0_at_1s;
  512.     else
  513.         select_brush(color);
  514.     } else {
  515.     if (one == gx_no_color_index) {
  516.         color = (int)zero;
  517.         rop = rop_write_at_0s;
  518.     } else {        /* Pre-clear the rectangle to zero */
  519.         fill_rect(x, y, w, h, zero);
  520.         color = (int)one;
  521.     }
  522.     select_brush(color);
  523.     }
  524.  
  525.     if (id != wdev->bm_id || id == gx_no_bitmap_id) {
  526.     wdev->bm_id = id;
  527.     if (raster == bmWidthBytes) {    /* We can do the whole thing in a single transfer! */
  528.         SetBitmapBits(wdev->hbmmono,
  529.               (DWORD) (bmWidthBytes * h),
  530.               (BYTE *) base);
  531.     } else {
  532.         for (height = h; height--;
  533.          ptr_line += raster, aptr += bmWidthBytes
  534.         ) {        /* Pack the bits into the bitmap. */
  535.         switch (width_bytes) {
  536.             default:
  537.             memcpy(aptr, ptr_line, width_bytes);
  538.             break;
  539.             case 4:
  540.             aptr[3] = ptr_line[3];
  541.             case 3:
  542.             aptr[2] = ptr_line[2];
  543.             case 2:
  544.             aptr[1] = ptr_line[1];
  545.             case 1:
  546.             aptr[0] = ptr_line[0];
  547.         }
  548.         }
  549.         SetBitmapBits(wdev->hbmmono,
  550.               (DWORD) (bmWidthBytes * h),
  551.               &aBit[0]);
  552.     }
  553.     }
  554.     BitBlt(wdev->hdcmf, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  555.     return 0;
  556. }
  557.  
  558.  
  559. /* Copy a color pixel map.  This is just like a bitmap, except that */
  560. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  561. private int
  562. win_prn_copy_color(gx_device * dev,
  563.         const byte * base, int sourcex, int raster, gx_bitmap_id id,
  564.            int x, int y, int w, int h)
  565. {
  566.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  567.  
  568.     if (gx_device_has_color(dev)) {
  569.     switch (dev->color_info.depth) {
  570.         case 8:
  571.         {
  572.             int xi, yi;
  573.             int skip = raster - w;
  574.             const byte *sptr = base + sourcex;
  575.  
  576.             if (w <= 0)
  577.             return 0;
  578.             if (x < 0 || x + w > dev->width)
  579.             return_error(gs_error_rangecheck);
  580.             for (yi = y; yi - y < h; yi++) {
  581.             for (xi = x; xi - x < w; xi++) {
  582.                 int color = *sptr++;
  583.  
  584.                 SetPixel(wdev->hdcmf, xi, yi, PALETTEINDEX(color));
  585.             }
  586.             sptr += skip;
  587.             }
  588.         }
  589.         break;
  590.         case 4:
  591.         {        /* color device, four bits per pixel */
  592.             const byte *line = base + (sourcex >> 1);
  593.             int dest_y = y, end_x = x + w;
  594.  
  595.             if (w <= 0)
  596.             return 0;
  597.             while (h--) {    /* for each line */
  598.             const byte *source = line;
  599.             register int dest_x = x;
  600.  
  601.             if (sourcex & 1) {    /* odd nibble first */
  602.                 int color = *source++ & 0xf;
  603.  
  604.                 SetPixel(wdev->hdcmf, dest_x, dest_y, PALETTEINDEX(color));
  605.                 dest_x++;
  606.             }
  607.             /* Now do full bytes */
  608.             while (dest_x < end_x) {
  609.                 int color = *source >> 4;
  610.  
  611.                 SetPixel(wdev->hdcmf, dest_x, dest_y, PALETTEINDEX(color));
  612.                 dest_x++;
  613.                 if (dest_x < end_x) {
  614.                 color = *source++ & 0xf;
  615.                 SetPixel(wdev->hdcmf, dest_x, dest_y, PALETTEINDEX(color));
  616.                 dest_x++;
  617.                 }
  618.             }
  619.             dest_y++;
  620.             line += raster;
  621.             }
  622.         }
  623.         break;
  624.         default:
  625.         return (-1);    /* panic */
  626.     }
  627.     } else
  628.     /* monochrome device: one bit per pixel */
  629.     {                /* bitmap is the same as win_copy_mono: one bit per pixel */
  630.     win_prn_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  631.               (gx_color_index) 0,
  632.               (gx_color_index) (dev->color_info.depth == 8 ? 63 : dev->color_info.max_gray));
  633.     }
  634.     return 0;
  635. }
  636.  
  637.  
  638. /* ------ Internal routines ------ */
  639.  
  640. #undef wdev
  641.  
  642.  
  643. private void near
  644. win_prn_addtool(gx_device_win_prn * wdev, int i)
  645. {
  646.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  647.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  648. }
  649.  
  650.  
  651. private void near
  652. win_prn_maketools(gx_device_win_prn * wdev, HDC hdc)
  653. {
  654.     int i;
  655.  
  656.     wdev->hpensize = (1 << (wdev->color_info.depth)) * sizeof(HPEN);
  657.     wdev->hpens = (HPEN *) gs_malloc(1, wdev->hpensize,
  658.                      "win_prn_maketools(pens)");
  659.     wdev->hbrushsize = (1 << (wdev->color_info.depth)) * sizeof(HBRUSH);
  660.     wdev->hbrushs = (HBRUSH *) gs_malloc(1, wdev->hbrushsize,
  661.                      "win_prn_maketools(brushes)");
  662.     if (wdev->hpens && wdev->hbrushs) {
  663.     for (i = 0; i < wdev->nColors; i++)
  664.         win_prn_addtool(wdev, i);
  665.  
  666.     wdev->hpen = wdev->hpens[0];
  667.     SelectObject(hdc, wdev->hpen);
  668.  
  669.     wdev->hbrush = wdev->hbrushs[0];
  670.     SelectObject(hdc, wdev->hbrush);
  671.     }
  672. }
  673.  
  674.  
  675. private void near
  676. win_prn_destroytools(gx_device_win_prn * wdev)
  677. {
  678.     int i;
  679.  
  680.     for (i = 0; i < wdev->nColors; i++) {
  681.     DeleteObject(wdev->hpens[i]);
  682.     DeleteObject(wdev->hbrushs[i]);
  683.     }
  684.     gs_free((char *)wdev->hbrushs, 1, wdev->hbrushsize,
  685.         "win_prn_destroytools(brushes)");
  686.     gs_free((char *)wdev->hpens, 1, wdev->hpensize,
  687.         "win_prn_destroytools(pens)");
  688. }
  689.